package cn.tedu.micro.demo.all.dao.repository;

import cn.tedu.micro.demo.all.dao.mapper.CartMapper;
import cn.tedu.micro.demo.pojo.po.CartPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CartRepositoryImpl implements CartRepository{
    @Autowired
    private CartMapper cartMapper;

    @Override
    public void save(CartPO po) {
        //当前注入到仓储层的 mapper对象 实例化 mybatis-plus做的,在实例的对象中,把继承自BaseMapper中
        //所有的单表CRUD方法全部都实现了,实现过程使用到了泛型CartPO 才能完整拼接一个可以用的sql语句
        System.out.println("cartMapper实现类:"+cartMapper.getClass().getName());
        cartMapper.insert(po);//insert into cart_tbl (id,user_id,product_code,count) values(#{},#{})
        //update delete select
    }

    @Override
    public List<CartPO> findMyCarts(String userId) {
        //select * from cart_tbl where user_id = #{userId} and count>3
        //搜索查询 关键点在于 where条件的拼接 鉴于 where条件的匹配逻辑非常之多 in = like >= <=
        //QueryWrapper类型是mybatis-plus常用的拼接where条件的 查询条件包装器
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        //like 模糊 in 集合 gt(greater than) lt(less than) gte(greater than equal) lte(less than equal)
        List myCarts = cartMapper.selectList(queryWrapper);
        return myCarts;
    }

    @Override
    public void deleteCart(String userId, String productCode) {
        //delete from cart_tbl where user_id=#{userId} and product_code=#{productCode}
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("product_code",productCode);
        cartMapper.delete(queryWrapper);
    }
}
